package com.hejia.alauda.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 数据安全
 * @author hyr
 *
 */
public class DataSecurity {
	
	/**
	 * 隐蔽公民身份号码
	 * @param idCard	公民身份号码
	 * @return			隐蔽的公民身份号码
	 */
	public static String maskIdCard(String idCard){
		
		if(StringUtils.isBlank(idCard)){
			return "";
		}
		
		int length = idCard.length();
		if(length <= 8){
			return idCard;
		}
		
		String maskIdCard = idCard.substring(0, 4);
		maskIdCard = StringUtils.rightPad(maskIdCard, length - 4, '*');
		maskIdCard += idCard.substring(length - 4, length);
		
		return maskIdCard;
	}
	
	/**
	 * 隐蔽人名
	 * @param name 姓名
	 * @return     隐蔽的姓名
	 */
	public static String maskPersonName(String name){
	
		if(StringUtils.isBlank(name)){
			return "";
		}
		
		String maskName = "";
		int length = name.length();
		if(length == 1){
			maskName = name;
		}else if(length == 2){
			maskName = Character.toString(name.charAt(0)) + "*";
		}else if(length == 3){
			maskName = Character.toString(name.charAt(0)) + "*" + Character.toString(name.charAt(2));
		}else if(length >= 4){
			maskName = name.substring(0, 2);
			for(int i = 2; i < length - 1; i++){
				maskName += "*";
			}
			maskName += name.substring(length -1 , length);
		}
		return maskName;
	}
	
	/**
	 * 隐蔽银行卡号
	 * @param cardNumber 银行卡号
	 * @return     隐蔽的银行卡号
	 */
	public static String maskBankCardNumber(String cardNumber){
	
		return DataSecurity.maskIdCard(cardNumber);
	}
}
